package com.example.toy.core.utils;

import java.util.regex.Pattern;

/**
 * PatternUtils
 *
 * @author shenbing
 * @date 2021/12/21
 */
public class TablePatternUtils {
  private static final Pattern ANY_STRING = Pattern.compile(".*");

  public static boolean match(Pattern pattern, String name) {
    if (pattern == null) return true;
    return pattern.matcher(name).matches();
  }

  public static Pattern mkPattern(String pattern) {
    if (pattern == null || pattern.isEmpty() || pattern.equals("%")) {
      return ANY_STRING;
    }
    String replaced = pattern.replaceAll("%", ".*");
    return Pattern.compile(replaced);
  }

  public static boolean match(String pattern, String name) {
    return match(mkPattern(pattern), name);
  }

  public static void main(String[] args) {
    System.out.println(match("%", "abcd"));
    System.out.println(match("", "abcd"));
    System.out.println(match((String) null, "abcd"));
    System.out.println(match("%%", "abcd"));

    System.out.println(match("ab%", "abcd"));
    System.out.println(match("%d", "abcd"));
    System.out.println(match("% d", "abcd"));

    System.out.println(match("a%d", "abcd"));
  }
}
